package com.ddfeiyu.agreement.codec;



import com.ddfeiyu.agreement.Packet;
import com.ddfeiyu.common.SerializationUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 编码器
 */
public class ObjEncoder extends MessageToByteEncoder<Packet> {
    private Logger logger = LoggerFactory.getLogger(ObjEncoder.class);

    @Override
    protected void encode(ChannelHandlerContext ctx, Packet in, ByteBuf out) {
        byte[] data = SerializationUtil.serialize(in);
        out.writeInt(data.length + 1);
        //添加指令

        Byte command = in.getCommand();
        Class<? extends Packet> clazz = Packet.get(command);
        logger.info("编码器ObjEncoder--,clazz:[ {} ]", clazz.getSimpleName());
        out.writeByte(in.getCommand());
        out.writeBytes(data);
    }

}
